const app = getApp();
const api = app.apiInstance;
//非通用组件~~~~~~
Component({

    properties: {
        dataList: {
            type: Array,
            value: [],
            observer: function (e) {
                console.log('e', e);
                this.setData({
                    dataList: e
                })

            }
        },
        multiple: {
            type: Boolean,
            value: false,
            observer: function (e) {

                this.setData({
                    multiple: e
                })
            }
        },
        area:{
            type: String,
            value: false,
            observer: function (e) {

                this.setData({
                    area: e
                })
            }
        },

        show: {
            type: Boolean,
            value: false,
            observer: function (e) {
               
                this.setData({
                    show: e,
                    currentStep:0
                })
            }
        },

    },

    data: {
        currentStep: 0,
        windowWidth: '',
        dataList: [],
        multiple: false,
        selectData: [],
        show: false,
        area:''
    },

    methods: {
        close() {
            this.setData({
                show: false
            })
        },

        selectStep(e){
 
            this.setData({
                currentStep: e.target.dataset.index
            })
        },

        swiperChange(e){
        },
        async searchSelect(e) {
            let area = encodeURIComponent(this.data.area);
            let baseUrl = `tissueArea/wx/getLevel1Area?area=`+area;
            const selectData = JSON.parse(JSON.stringify(this.data.selectData));
            const index = e.currentTarget.dataset.index;
            selectData[index] = e.detail;
            selectData[index].ishidden && delete selectData[index].ishidden;
            this.setData({
                currentStep: index
            })
            let url = '';
            if (index == 0) {
                let area1 = encodeURIComponent(e.detail.name);
                url = '/tissueArea/wx/getLevel2Area?area='+area+'&level1Area='+area1;
                delete selectData[1];
                delete selectData[2];
            }
            else if (index == 1) {
                let area1 = encodeURIComponent(selectData[0].name);
                let area2 = encodeURIComponent(e.detail.name);
                url = '/tissueArea/wx/getLevel3Area?area='+area+'&level1Area='+area1+'&level2Area='+area2+''
                delete selectData[2];
            }

            if (index !=2) {
                const res = await api.get(url);
                if(res.data.length==0){
                    //没有数据说明中止了
                    this.triggerEvent("select",selectData);
                    this.setData({
                        show: false
                    })

                }
                const result = res.data.map(item => { return { name: item } })
                const dataList = JSON.parse(JSON.stringify(this.data.dataList));
                dataList[index + 1] = result;
                console.log('dataList', dataList);


                this.setData({
                    dataList: dataList
                })

             
            }

          

            setTimeout(() => {
                let currentStep = this.data.currentStep

                this.setData({
                    selectData: selectData,
                })
                if ((index + 1) != this.data.dataList.length) {
                    this.setData({
                        currentStep: currentStep + 1, 
                    })
                   
                }
                else {
                    this.triggerEvent("select",this.data.selectData);
                    this.setData({
                        show: false
                    })
                }

              
            }, 0)


        },

        submit(){

         
            this.triggerEvent("select",this.data.selectData);
            this.setData({
                show: false
            })
        },
    },

   


    pageLifetimes: {
        /**
         * 生命周期函数--监听页面加载
         */
        show: function (options) {

            var systemInfo = wx.getSystemInfoSync();
            this.setData({
                windowWidth: systemInfo.windowWidth
            })


        },
        created: function () {

        },
        attached: function () {

        },
        ready: function () {
        },
        moved: function () {

        },
        detached: function () {

        }
    }
});
